package com.kamistoat.meimeimall.meimeimallproduct.aliyunVideo.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import com.kamistoat.meimeimall.common.To.AliyunVideoTo.SaveAliVideoInfoTo;
import com.kamistoat.meimeimall.common.To.AliyunVideoTo.VideoUploadAuthRequestTo;
import com.kamistoat.meimeimall.common.To.AliyunVideoTo.VideoUploadAuthResponseTo;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallproduct.aliyunVideo.InitObject;
import com.kamistoat.meimeimall.meimeimallproduct.aliyunVideo.service.GetVideoUploadService;
import com.kamistoat.meimeimall.meimeimallproduct.entity.VideoEntity;
import com.kamistoat.meimeimall.meimeimallproduct.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class GetVideoUploadServiceImpl implements GetVideoUploadService {

    @Value("${spring.cloud.alicloud.access-key}")
    private String accessId;
    @Value("${spring.cloud.alicloud.secret-key}")
    private String secret;

    @Autowired
    VideoService videoService;

    /**
     * 后端与阿里云通信，获取视频上传地址和凭证
     *
     * @param videoUploadAuthRequestTo
     * @return
     */
    @Override
    public VideoUploadAuthResponseTo getVideoUpload(VideoUploadAuthRequestTo videoUploadAuthRequestTo) throws ClientException {
        // 新建链接客户端
        DefaultAcsClient defaultAcsClient = InitObject.initVodClient(accessId, secret);
        // 新建一个上传视频请求库类，为这个请求设置参数
        CreateUploadVideoRequest uploadRequest = new CreateUploadVideoRequest();

        // 把前端传递来的参数先设置上。
        uploadRequest.setTitle(videoUploadAuthRequestTo.getTitle());
        uploadRequest.setFileName(videoUploadAuthRequestTo.getFileName());
        uploadRequest.setDescription(videoUploadAuthRequestTo.getDescription());

        // TODO: 根据前端的转码设置，新建转码组
        String definition = videoUploadAuthRequestTo.getReformatClear();
        String codec = videoUploadAuthRequestTo.getReformatType();

        // 后端与阿里云进行通信，获得视频上传的地址和凭证
        CreateUploadVideoResponse acsResponse = defaultAcsClient.getAcsResponse(uploadRequest);

        // 将地址凭证放到To中，准备返回给前端
        VideoUploadAuthResponseTo uploadResponseTo = new VideoUploadAuthResponseTo();
        uploadResponseTo.setRequestId(acsResponse.getRequestId());
        uploadResponseTo.setVideoId(acsResponse.getVideoId());
        uploadResponseTo.setUploadAddress(acsResponse.getUploadAddress());
        uploadResponseTo.setUploadAuth(acsResponse.getUploadAuth());

        // TODO: 转码完成后，删除掉刚刚的转码组

        return uploadResponseTo;
    }

    /**
     * 后端与阿里云通信，刷新过期的上传地址和凭证
     *
     * @param videoId 暂停上传的videoId
     * @return
     */
    @Override
    public VideoUploadAuthResponseTo refreshVideoUpload(String videoId) throws ClientException {
        // 新建链接客户端
        DefaultAcsClient defaultAcsClient = InitObject.initVodClient(accessId, secret);
        // 新建刷新凭证请求
        RefreshUploadVideoRequest refreshRequest = new RefreshUploadVideoRequest();
        refreshRequest.setVideoId(videoId);
        // 与阿里云通信刷新上传地址和凭证
        RefreshUploadVideoResponse acsResponse = defaultAcsClient.getAcsResponse(refreshRequest);

        // 将地址凭证放到To中，准备返回给前端
        VideoUploadAuthResponseTo uploadResponseTo = new VideoUploadAuthResponseTo();
        uploadResponseTo.setRequestId(acsResponse.getRequestId());
        uploadResponseTo.setVideoId(acsResponse.getVideoId());
        uploadResponseTo.setUploadAddress(acsResponse.getUploadAddress());
        uploadResponseTo.setUploadAuth(acsResponse.getUploadAuth());

        return uploadResponseTo;
    }

    /**
     * 前端直连阿里云上传视频完成后，后端将视频信息保存到数据库中
     *
     * @param infoTo
     * @return
     */
    @Override
    public R saveAliVideoInfo(SaveAliVideoInfoTo infoTo) {
        // 将VideoEntity中的字段填充完整
        VideoEntity videoEntity = new VideoEntity();
        BeanUtils.copyProperties(infoTo, videoEntity);


        // 从阿里云上获取视频封面信息
        GetVideoInfoRequest getVideoInfoRequest = new GetVideoInfoRequest();
        getVideoInfoRequest.setVideoId(videoEntity.getVideoIdCloud());

        String coverUrl = "";

        try {
            while (StringUtils.isEmpty(coverUrl)) {
                DefaultAcsClient defaultAcsClient = InitObject.initVodClient(accessId, secret);
                GetVideoInfoResponse infoResponse = defaultAcsClient.getAcsResponse(getVideoInfoRequest);
                coverUrl = infoResponse.getVideo().getCoverURL();
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        // TODO:其实这里可以根据阿里云上的视频信息和前端发来的做二次对比。对比成功再入数据库

        videoEntity.setCoverUrl(coverUrl);
        videoEntity.setStoreType(0);
        videoEntity.setShowStatus(1);
        Date curtime = new Date();
        videoEntity.setUploadTime(curtime);
        // 直接调用mybatis的集成方法进行保存
        videoService.save(videoEntity);
        return R.ok();
    }
}
